package com.niuxiaofa.newlaw.Tools;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebviewTool {


    @SuppressLint("JavascriptInterface")
    public void initWebView(WebView mWebView, String url) {
        WebSettings settings = mWebView.getSettings();
        mWebView.clearCache(true);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(true);
        mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        mWebView.getSettings().setAppCacheEnabled(false);//是否使用缓存

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据。

        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);

        mWebView.loadUrl(url);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWebView.loadUrl(url);


                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

             //   Log.e("---","onPageFinished");


                mWebView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.evaluateJavascript( "var objs = document.getElementsByTagName(\"img\"); " +
                                "var imgUrl = \"\";" +
                                "for(var i=0;i<objs.length;i++){" +
                                "imgUrl += objs[i].src + ',';" +
                                "console.log(\"----222--\"+imgUrl);" +
                                "    objs[i].onclick=function() " +
                                "    {  "+
                                "console.log(\"--onclick--\"+imgUrl);" +
                                "       window.android.openImage(imgUrl,this.src);" +
                                "    }" +
                                "}", null);
                    }
                }, 1000);



//                //网页加载完成 走JS代码
//                mWebView.loadUrl(
//                        "javascript:(function(){" +
//                                "var objs = document.getElementsByTagName(\"img\"); " +
//                                "var imgUrl = \"\";" +
//                                "for(var i=0;i<objs.length;i++){" +
//                                "imgUrl += objs[i].src + ',';" +
//                                "console.log(\"----222--\"+imgUrl);" +
//                                "    objs[i].onclick=function() " +
//                                "    {  "+
//                                        "console.log(\"--onclick--\"+imgUrl);" +
//                                 "       window.android.openImage(imgUrl,this.src);" +
//                                "    }" +
//                                "}" +
//                                "})()"
//                );



            }

            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //handler.cancel(); 默认的处理方式，WebView变成空白页
//		                  //接受证书
                Log.e("--onReceivedSslError","onReceivedSslError");
                handler.proceed();
                //handleMessage(Message msg); 其他处理
            }

        });



        mWebView.addJavascriptInterface(this, "android");


    }


}
